package com.zhongbei.controller;

import com.zhongbei.mapper.UserMapper;
import com.zhongbei.pojo.User;
import com.zhongbei.service.UserService;
import com.zhongbei.utils.Result;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zhongbei")
public class UserController {

 
    @Autowired
    private UserService userService;


//    1.增 一条用户数据
    @GetMapping("/insertOne")
    public Result<String>insertOne(User user){
//        判断
        int i = userService.insertOne(user);
        if (i==1){
            return Result.success("新增成功");
        }else {
            return Result.error("新增失败");
        }
    }

//    2.删  根据id删除数据
    @GetMapping("/deleteUserById")
    public Result<String> deleteUserById(int id){
         int i = userService.deleteUserById(id);
         if (i==1){
             return Result.success("删除成功");
         }else {
             return Result.error("删除失败");
         }
    }

//    3.改 根据id修改数据
    @GetMapping("/updateById")
    public Result<String> updateById(User user){
        int i = userService.updateById(user);
        if (i==1){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    //4.查 查全部信息
    @GetMapping("/getAllUser")
    public Result<List<User>> getAllUser(){
        List<User> allUser = userService.getAllUser();
        if (allUser.size()!=0){
            return Result.success(allUser);
        }else {
            return  Result.error("查询失败");
        }

    }
    //5.通过name获取用户信息
    @GetMapping("/userByName")
    public Result<User> userByName(String name){
        User user =  userService.userByName(name);
        return Result.success(user);
    }


     //6.登录验证
    //登录
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
     int count =  userService.isLogin(user);
    // select  count(*)  from 表名  where  username  password
        if(count==1){
            return Result.success("登陆成功");
        }else {
            return Result.error("登录失败，重新登录");
        }
    }



}
